package com.lite.haru.alarmpolice.fragment;

import java.util.ArrayList;
import java.util.List;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.ui.KJFragment;

import com.alibaba.fastjson.JSON;
import com.lite.haru.R;
import com.lite.haru.alarmpolice.activity.AlarmInfoDetailAty;
import com.lite.haru.constant.Constant;
import com.lite.haru.constant.PoliceApplication;
import com.lite.haru.entity.AlarmInfo;
import com.lite.haru.myadapter.MyAlarmInfoProcessedAdp;
import com.lite.haru.tools.CheckNetWork;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class AlarmProcessedFrag extends KJFragment {
	private KJHttp kjh;
	private int police_id;// 当前登录app警员的id
	private PoliceApplication mApp;

	private List<AlarmInfo> alarm_processed_info_datas;// 已处理的数据

	@BindView(id = R.id.alarm_info_listview)
	private ListView alarm_info_listview;
	@BindView(id = R.id.network)
	private TextView network;
	@BindView(id = R.id.data_empty)
	private TextView data_empty;
	// 已处理信息的适配器
	private MyAlarmInfoProcessedAdp myAlarmInfoProcessedAdapter;

	@Override
	protected View inflaterView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
		View view = View.inflate(getActivity(),
				R.layout.frag_alarm_info_processed, null);
		return view;
	}

	@Override
	protected void initData() {
		kjh = new KJHttp();
		mApp = (PoliceApplication) getActivity().getApplication();
		police_id = mApp.police_id;
		alarm_processed_info_datas = new ArrayList<AlarmInfo>();
		super.initData();
	}

	@Override
	protected void initWidget(View parentView) {

		forProcessedData();

		OnItemClick();
		super.initWidget(parentView);
	}

	public void forProcessedData() {
		if (CheckNetWork.isNetworkAvailable(getActivity())) {
			String url = Constant.url + "/AlarmInfoAction";
			HttpParams params = new HttpParams();
			params.put("param0", "get_processed_alarm_info");
			params.put("police_id", police_id);

			kjh.post(url, params, false, new HttpCallBack() {

				@Override
				public void onSuccess(String t) {
					alarm_processed_info_datas = JSON.parseArray(t,
							AlarmInfo.class);
					if (alarm_processed_info_datas.isEmpty()
							|| alarm_processed_info_datas.equals(null)) {
						data_empty.setVisibility(View.VISIBLE);
					} else {
						data_empty.setVisibility(View.GONE);
						myAlarmInfoProcessedAdapter = new MyAlarmInfoProcessedAdp(
								alarm_info_listview,
								alarm_processed_info_datas,
								R.layout.item_alarm_info);
						alarm_info_listview
								.setAdapter(myAlarmInfoProcessedAdapter);
					}
					super.onSuccess(t);
				}

				@Override
				public void onFailure(int errorNo, String strMsg) {
					super.onFailure(errorNo, strMsg);
				}
			});
		} else {
			data_empty.setVisibility(View.GONE);
			network.setVisibility(View.VISIBLE);
		}
	}

	// 列表子选项的点击事件
	public void OnItemClick() {
		alarm_info_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				AlarmInfo a = alarm_processed_info_datas.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt("id", a.getId());
				bundle.putString("username", a.getUsername());
				bundle.putString("phone", a.getPhone());
				bundle.putString("address", a.getAddress());
				bundle.putString("contactperson", a.getContactperson());
				bundle.putString("contactpersonphone",
						a.getContactpersonphone());
				bundle.putString("alarmtype", a.getAlarmtype());
				bundle.putString("alarmwork", a.getAlarmwork());
				bundle.putString("disaster", a.getDisaster());
				bundle.putString("grade", a.getGrade());
				bundle.putDouble("latitude", a.getLatitude());
				bundle.putDouble("longitude", a.getLongitude());
				bundle.putString("formatted_address", a.getFormatted_address());
				bundle.putString("sematic_description",
						a.getSematic_description());
				bundle.putString("flag", a.getFlag());
				Intent intent = new Intent(getActivity(),
						AlarmInfoDetailAty.class);
				intent.putExtras(bundle);
				startActivity(intent);

			}
		});

	}

}
